package com.huawei.appmarket.service.installresult.control;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build.VERSION;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.bean.startup.StartupResponse;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.service.uninstallreport.UninstallRecord;
import com.huawei.appmarket.service.uninstallreport.UninstallReportJobService;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.util.List;
import o.bjx;
import o.ccz;
import o.cdb;
import o.cdg;
import o.cdx;
import o.cfw;
import o.HcridSession;
import o.cnv;
import o.BaseRequest;
import o.cvv;
import o.diu;
import o.Logger;
import o.dof;
import o.ContentManager;
import o.dpp;
import o.dss;
import o.dsv;
import o.dsz;
import o.dtd;
import o.dte;
import o.duj;
import o.dul;
import o.dvt;
import o.eau;
import o.eaw;
import o.ekc;
import o.CollectCheck;
import o.eub;
import o.euc;
import o.eug;
import o.eun;
import o.fho;
import o.fhz;

public class InstallerReceiver extends BroadcastReceiver {
    private static boolean ˋ = false;
    private static InstallerReceiver ˏ = new InstallerReceiver();

    static class a implements Runnable {
        private String ˊ;
        private UninstallRecord ˎ;

        public a(UninstallRecord uninstallRecord, String str) {
            this.ˎ = uninstallRecord;
            this.ˊ = str;
        }

        public void run() {
            if (ekc.ˋ().ˏ(this.ˎ) < 0) {
                Logger.e("InstallerReceiver", "saveForReport error");
            } else {
                Logger.ˎ("InstallerReceiver", "saveForReport package=" + this.ˊ);
            }
        }
    }

    static class c implements IServerCallBack {
        private c() {
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
            if ((responseBean instanceof StartupResponse) && responseBean.getRtnCode_() == 0 && responseBean.getResponseCode() == 0) {
                ((StartupResponse) responseBean).ॱˎ();
            }
        }
    }

    private static boolean ˋ() {
        return VERSION.SDK_INT >= 26 && !bjx.ˎ;
    }

    public static void register(Context context) {
        if (ˋ() && context != null) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.intent.action.PACKAGE_ADDED");
            intentFilter.addAction("android.intent.action.PACKAGE_REMOVED");
            intentFilter.addAction("android.intent.action.PACKAGE_CHANGED");
            intentFilter.addDataScheme(HiAnalyticsConstant.BI_KEY_PACKAGE);
            try {
                context.registerReceiver(ˏ, intentFilter);
            } catch (Throwable e) {
                Logger.ˋ("InstallerReceiver", "registerReceiver.", e);
            }
        }
    }

    public static void unregister(Context context) {
        if (ˋ() && context != null) {
            try {
                context.unregisterReceiver(ˏ);
            } catch (Throwable e) {
                Logger.ˋ("InstallerReceiver", "unregisterReceiver.", e);
            }
        }
    }

    public static boolean isReportUninstall() {
        return ˋ;
    }

    public static void setIsReportUninstall(boolean z) {
        ˋ = z;
    }

    public void onReceive(Context context, Intent intent) {
        if (ˋ()) {
            eau.ॱ(context, new SafeIntent(intent), this);
        } else {
            onReceiveNoRepeat(context, new SafeIntent(intent));
        }
    }

    public void onReceiveNoRepeat(Context context, SafeIntent safeIntent) {
        String str;
        String action = safeIntent.getAction();
        if (safeIntent.getDataString() == null || safeIntent.getDataString().length() < 9) {
            str = "";
        } else {
            str = safeIntent.getDataString().substring(8);
        }
        Logger.ˎ("InstallerReceiver", "InstallerReceiver onReceive action: " + action + ", packageName: " + str);
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            cnv o_cnv = (cnv) ˋ.ॱ(cnv.class);
            if (o_cnv != null) {
                o_cnv.ˏ(context, safeIntent, 0);
            }
        }
        euc.ˊ().ॱ();
        if ("android.intent.action.PACKAGE_REMOVED".equals(action)) {
            ˋ(safeIntent, str);
            dss.ॱ().ʻ(str);
            dsv.ॱ(str, 103);
            dsz.ˊ(str);
            dte.ॱ().ˏ(str);
            dpp.ˏ().ˋ(str);
            ˊ(safeIntent, str);
            ˎ(str);
            eug.ˏ().ˋ(str);
            eun.ˏ().ˏ(str);
        } else if ("android.intent.action.PACKAGE_ADDED".equals(action)) {
            ˏ(context, action, str);
        } else if ("android.intent.action.PACKAGE_REPLACED".equals(action)) {
            dsv.ॱ(str, 102);
        }
        euc.ˊ().ˏ();
    }

    private void ˊ(Intent intent, String str) {
        boolean booleanExtra = intent.getBooleanExtra("android.intent.extra.DATA_REMOVED", false);
        boolean booleanExtra2 = intent.getBooleanExtra("android.intent.extra.REPLACING", false);
        Logger.ˎ("InstallerReceiver", "Cancel download packageName: " + str + ", isDataRemoved: " + booleanExtra + ", isReplacing: " + booleanExtra2);
        if (!booleanExtra2) {
            duj.ˏ().ˎ(dul.class, str);
        }
        List<SessionDownloadTask> ˏ = dvt.ˏ().ˏ(str);
        if (!CollectCheck.isEmpty((List) ˏ)) {
            for (SessionDownloadTask sessionDownloadTask : ˏ) {
                if (!booleanExtra2) {
                    dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
                }
            }
        }
    }

    private void ˏ(Context context, String str, String str2) {
        if (dtd.ˎ().ˏ() && dtd.ˎ().ॱ(str2) == null && ((cdg) cdb.ॱ(cdg.class)).ॱॱ(str2)) {
            Logger.ˎ("InstallerReceiver", "receive action_package_added broadcast, add app again, packageName = " + str2);
            dsz.ˋ(str2);
        }
        if (dte.ॱ().ˎ() && ((cdg) cdb.ॱ(cdg.class)).ॱॱ(str2)) {
            Logger.ˎ("InstallerReceiver", "receive action_package_added broadcast, add app to last used info list, packageName = " + str2);
            dte.ॱ().ˎ(str2);
        }
        dsv.ॱ(str2, 102);
        dss.ॱ().ॱॱ(str2);
        eub.ˎ().ʼ(str2);
        boolean ˎ = diu.ˊ().ˎ();
        if (Logger.ˎ()) {
            Logger.ˏ("InstallerReceiver", "onReceive " + str + ",agree protocol flag is " + ˎ);
        }
        if (ˎ) {
            new eaw(str2, o.eaw.c.COMMAND_CANCLE_DOWNLOADING_TASK).start();
            ˎ(str2);
        }
    }

    private void ˋ(SafeIntent safeIntent, String str) {
        if (ˋ && diu.ˊ().ˎ()) {
            UninstallReportJobService.schedule(ContentManager.getInstant().getContext());
            boolean booleanExtra = safeIntent.getBooleanExtra("android.intent.extra.REPLACING", false);
            long currentTimeMillis = System.currentTimeMillis();
            if (!booleanExtra && !dof.ˋ(str)) {
                UninstallRecord uninstallRecord = new UninstallRecord();
                uninstallRecord.ˎ(str);
                uninstallRecord.ˎ(currentTimeMillis);
                cdx.ॱ(new a(uninstallRecord, str));
            }
        }
    }

    private void ˎ(String str) {
        if ("com.google.android.gms".equals(str) || "com.google.android.gsf".equals(str) || "com.android.vending".equals(str)) {
            HcridSession.ˋ().ˏ("", ccz.ॱ());
            HcridSession.ˋ().ˏ("", 18);
            HcridSession.ˋ().ˏ("", 17);
            cfw.ˎ(cvv.ˎ(), new c());
        }
    }
}
